package priv.yiyuan.example.composite.file;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件夹类
 * <p>
 * 组合模式-组合节点
 *
 * @author YiYuan
 * @data 2025/8/29
 * @apoNote
 */
public class Folder extends DocumentComponent {

    private List<DocumentComponent> children = new ArrayList<>();

    private String permission;

    public Folder(String name) {
        super(name);
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getPermission() {
        return permission;
    }

    @Override
    public void add(DocumentComponent component) {
        children.add(component);
        component.setParent(this);
    }

    @Override
    public void remove(DocumentComponent component) {
        children.remove(component);
        component.setParent(null);
    }

    @Override
    public List<DocumentComponent> getChildren() {
        return this.children;
    }

    @Override
    public void display() {
        System.out.println("文件夹: " + name);
        for (DocumentComponent component : children) {
            component.display();
        }
    }

    @Override
    public boolean checkPermission(String user) {
        if (permission != null) {
            System.out.println("检查文件夹 " + name + " 的权限: " + permission);
            return permission.contains(user);
        }

        if (parent != null) {
            return parent.checkPermission(user);
        }

        return true;
    }
}
